<?php
namespace app\houtai\controller\system\channel;
use app\houtai\controller\BaseController;

class VersionController extends BaseController {
  const REMARK_MIN = 10;
  const REMARK_MAX = 4000;
  /**
   *  @brief  : 后台-左侧菜单
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    $CHANNELS = $this->GetAdminChannel();
    ////////////////////////////////////////////////////////////////////////////
    $channel = $this->request->param('channel/d', -1);
    if ($channel > 0 && array_key_exists($channel, $CHANNELS)) {
      $conds['Channel'][] = ['IN', array_keys($CHANNELS)];
      $conds['Channel'][] = ['EQ', $channel];
    } else {
      $conds['Channel'] = ['IN', array_keys($CHANNELS)];
    }
    //////////////////////////////////////
    $orderMap = [
      'A' => 'ID',
      'B' => 'Channel',
      'C' => 'VOld',
      'D' => 'VNew',
      'E' => 'CreateTime',
      'F' => 'Remark',
    ];
    $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'E';
    $order[$orderMap[$this->orderField]] = $this->orderDirection;
    //////////////////////////////////////
    $list = table('GameChannelVersion', 'rydb.record')->where($conds)->order($order)->paginate();
    /////////////////////////////////////
    // 合计/今日'today'/昨天'yesterday'/本周'week'/上周'last week'/本月'month'/上月'last month'
    $info = [];
    $info['D']    = table('GameChannelVersion', 'rydb.record')->where($conds)->count();
    $info['D0']   = table('GameChannelVersion', 'rydb.record')->where($conds)->whereTime('CreateTime', 'today')->count();
    $info['D1']   = table('GameChannelVersion', 'rydb.record')->where($conds)->whereTime('CreateTime', 'yesterday')->count();
    $info['D7']   = table('GameChannelVersion', 'rydb.record')->where($conds)->whereTime('CreateTime', 'week')->count();
    $info['D14']  = table('GameChannelVersion', 'rydb.record')->where($conds)->whereTime('CreateTime', 'last week')->count();
    $info['D30']  = table('GameChannelVersion', 'rydb.record')->where($conds)->whereTime('CreateTime', 'month')->count();
    $info['D60']  = table('GameChannelVersion', 'rydb.record')->where($conds)->whereTime('CreateTime', 'last month')->count();
    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'list'      => $list,
      'info'      => $info,
      'CHANNELS'  => $CHANNELS,
      'channel'   => $channel,
    ]);
  }
  /**
   *  @brief  : 改版本号
   *  @return : PAGE|JSON
   *  @details:
   */
  public function detailAction() {

    $id = $this->request->param('id/d', 0);
    if ($id > 0) {
      $Channel = table('ChannelConfig', 'rydb.basic')->where(['ID' => $id])->find();
    }
    if ($this->request->isGet()) {
      if (!empty($Channel)) {
        $LasterTime = table('GameChannelVersion', 'rydb.record')->where('Channel', $Channel['ID'])->order(['CreateTime' => 'DESC'])->value('CreateTime');
        $Channel['LasterTime'] = $LasterTime;
      } else {
        $Channel = [];
      }
      return $this->fetch('', ['row' => $Channel, 'REMARK_MIN' => self::REMARK_MIN, 'REMARK_MAX' => self::REMARK_MAX]);
    }
    if ($this->request->isPost()) {
      if (empty($Channel)) {
        return result([], 100, '更新失败!欲修改的渠道不存在!');
      }
      $Version = $this->request->param('Version/s','','trim');
      // return result(['data'=>$Version], 101, '更新失败!版本号无参数!');
      // die;
      if ($Version == '') {
        return result([], 101, '更新失败!版本号无参数!');
      }
      $Remark = $this->request->param('Remark/s', '', 'trim');
      $len = strlen($Remark);
      if ($len < self::REMARK_MIN || $len > self::REMARK_MAX) {
        return result([], 102, '更新失败!更新说明长度不符合!请检查!');
      }
      if ($Channel['Version'] == $Version) {
        return result([], 103, '更新失败!版本号未发生变动!');
      }
      /*
      if ($Version <= $Channel['Version']) {
        return result([], 106, "更新失败!版本号必须大于当前版本号[{$Channel['Version']}]");
      }
      */
        //http://47.96.122.75:4775/monitor?name=UpdateVersion&NewVer=10
      // 通知游戏服务器
      try {
        $params = [
          'name'    => 'UpdateVersion',
          'NewVer'  => $Version,
        ];
        $addr = substr($Channel['ID'],1,4);
        $retn = CallGoSrv($params,$addr);
      } catch (Exception $e) {
        $error = $e->getMessage();
        $errno = $e->getCode();
        return result([], $errno, "更新版本号成功, 通知游戏服务器失败[{$error}]");
      }

      $eax = table('ChannelConfig', 'rydb.basic')->where(['ID' => $Channel['ID']])->setField('Version', $Version);
      $eax = table('GameChannelVersion', 'rydb.record')->insert([
        'Channel' => $Channel['ID'], 'VOld' => $Channel['Version'], 'VNew' => $Version, 'Remark' => $Remark,
      ]);
      return result([], 0);
    }
  }
}
